package com.zmn.plat.admin.controller.orderconfig.comment;

import java.util.List;
import java.util.Optional;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.orderconfig.comment.OrderComment;
import com.zmn.plat.model.entity.orderconfig.comment.OrderCommentQuery;

/**
 * @author tanbiao
 * @description
 * @since 2019/10/25 9:12
 * @modifier
 **/
@Controller
@RequestMapping("/orderconfig/comment/")
public class OrderCommentListController extends OrderCommentBaseController {
    private Logger logger = LoggerFactory.getLogger(OrderCommentListController.class);

    /**
     * 描述: 订单取消分类列表
     *
     * @param query
     *            查询实体
     * @return ModelAndView
     * @author tanbiao
     * @since 2019/10/22
     */
    @RequestMapping(value = "list")
    @RequiresPermissions("/orderconfig/comment/list.action")
    public ModelAndView list(@ModelAttribute OrderCommentQuery query) {
        ModelAndView mav = new ModelAndView("orderconfig/comment/list");
        int count = orderCommentService.countByQuery(query);
        query.setTotalCount(count);
        List<OrderComment> cancelCategories = orderCommentService.listPageByQuery(query);
        mav.addObject("items", cancelCategories);
        mav.addObject("vo", query);
        mav.addObject("bizTypeList", com.zmn.plat.common.dictionary.ProductDict.listBizType());
        mav.addObject("commentTypeList", ProductDict.getOrderCommentTypeList());
        mav.addObject("stars", ProductDict.getOrderCommentStarsList());
        return mav;
    }

    /**
     * 功能描述 ：详情页面
     *
     * @param id
     * @return org.springframework.web.servlet.ModelAndView
     * @author tanbiao
     * @since 2019/10/22
     * @modifier
     */
    @RequestMapping(value = "detail")
    @RequiresPermissions("/orderconfig/comment/detail.action")
    public ModelAndView detail(@RequestParam(value = "id", defaultValue = "0") Integer id) {
        ModelAndView mav = new ModelAndView("orderconfig/comment/detail");

        OrderComment orderComment = new OrderComment();
        if (id == null) {
            orderComment = new OrderComment();
        } else {
            orderComment = Optional.ofNullable(orderCommentService.findByKey(id)).orElse(orderComment);
        }
        if (orderComment.getSort() == null) {
            Integer sort = orderCommentService.findMaxSort();
            sort = sort == null ? 1 : sort + 1;
            orderComment.setSort(sort);
        }
        // 取消分类
        mav.addObject("comment", orderComment);
        mav.addObject("commentTypeList", ProductDict.getOrderCommentTypeList());
        mav.addObject("stars", ProductDict.getOrderCommentStarsList());
        mav.addObject("bizTypeList", com.zmn.plat.common.dictionary.ProductDict.listBizType());
        return mav;
    }
}
